<template>
  <div class="box">
    <div class="header">
      <input placeholder="输入用户ID" type="text" v-model="userNo" />
      <button @click="search">搜索</button>
      <button @click="add">获得金币</button>
    </div>
    <LineChart :chart-data="lineChartData" width="900px" height="300px" />
    <MyEcharts :options="options" />
    <div>用户列表</div>
    <div v-for="item in members" :key="item.userNo">
      <CardItem :mm="item" />
    </div>
  </div>
</template>

<script setup lang="ts">
import CardItem from '@/components/CardItem.vue'
import LineChart from '@/components/LineChart.vue'
import MyEcharts from '@/components/MyEcharts.vue'
import { ref } from 'vue'
import type { MemberType } from '../common/type'
import { useMembers } from '../stores/members'
import { useStore } from 'vuex'

const { members } = useMembers()
const userNo = ref<string>('')
const member = ref<MemberType>()

const data = {
  newVisitis: {
    expectedData: [100, 120, 161, 134, 105, 160, 165],
    actualData: [120, 82, 91, 154, 162, 140, 145],
  },
  messages: {
    expectedData: [200, 192, 120, 144, 160, 130, 140],
    actualData: [180, 160, 151, 106, 145, 150, 130],
  },
  purchases: {
    expectedData: [80, 100, 121, 104, 105, 90, 100],
    actualData: [120, 90, 100, 138, 142, 130, 130],
  },
  shoppings: {
    expectedData: [130, 140, 141, 142, 145, 150, 160],
    actualData: [120, 82, 91, 154, 162, 140, 130],
  },
}
const lineChartData = ref(data.newVisitis)

const options = {
  title: {
    text: 'Stacked Line',
  },
  tooltip: {
    trigger: 'axis',
  },
  legend: {
    data: ['Email', 'Union Ads', 'Video Ads', 'Direct', 'Search Engine'],
  },
  grid: {
    left: '3%',
    right: '4%',
    bottom: '3%',
    containLabel: true,
  },
  toolbox: {
    feature: {
      saveAsImage: {},
    },
  },
  xAxis: {
    type: 'category',
    boundaryGap: false,
    data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
  },
  yAxis: {
    type: 'value',
  },
  series: [
    {
      name: 'Email',
      type: 'line',
      stack: 'Total',
      data: [120, 132, 101, 134, 90, 230, 210],
    },
    {
      name: 'Union Ads',
      type: 'line',
      stack: 'Total',
      data: [220, 182, 191, 234, 290, 330, 310],
    },
    {
      name: 'Video Ads',
      type: 'line',
      stack: 'Total',
      data: [150, 232, 201, 154, 190, 330, 410],
    },
    {
      name: 'Direct',
      type: 'line',
      stack: 'Total',
      data: [320, 332, 301, 334, 390, 330, 320],
    },
    {
      name: 'Search Engine',
      type: 'line',
      stack: 'Total',
      data: [820, 932, 901, 934, 1290, 1330, 1320],
    },
  ],
}

function search() {
  const target = members.find((m: MemberType) => m.userNo === userNo.value)
  if (target) member.value = { ...target }
}

const store = useStore()
async function add() {
  store.commit('addCoins')
  await store.dispatch('asyncAddCoins')
}
</script>

<style scoped>
.box {
  display: flex;
  flex-direction: column;
}

.header {
  display: flex;
  flex-direction: row;
  margin: 10px 5px;
}

.result {
  display: flex;
}

.noResult {
  display: none;
}
</style>
